package com.example.kuaizu.service.imp;

import com.example.kuaizu.mapper.SubscribeMassageMapper;
import com.example.kuaizu.pojo.SubMassageParam;
import com.example.kuaizu.pojo.pram.JoinSubscribeMassageParam;
import com.example.kuaizu.service.InSubscribeMassageService;
import com.example.kuaizu.utils.ThreadLocalUtil;
import kuaizu.common.massage.MassageTemplateConst;
import kuaizu.common.tablepojo.SubscribeMessage;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * @author: wangmx15
 * @date: 2025/8/24
 **/
@Service
@RequiredArgsConstructor
public class SubscribeMassageService implements InSubscribeMassageService {
    private static final Logger log = LoggerFactory.getLogger(SubscribeMassageService.class);
    private final SubscribeMassageMapper subscribeMassageMapper;

    /**
     *有人投递项目时的订阅消息
      * @param req
     */
   @Override
    @Transactional
    public void subscribe(SubMassageParam req) {
       //投递通知  项目id(投递的项目) 跳转路径
       SubscribeMessage subMassageObject = createSubMassageObject(req.getProjectId(), req.getPageUrl(), MassageTemplateConst.MASSAGE_TYPE_DELIVERY, MassageTemplateConst.PROJECT_DELIVERY_TEMPLATE_ID);
       subscribeMassageMapper.addSubscribeMassage(subMassageObject);
       log.info("插入到消息订阅表");
    }

    /**
     * 项目审核通过之后的订阅消息
     * @param req
     */
    @Override
    @Transactional
    public void shenHeMassage(SubMassageParam req) {
       //项目审核  项目id 跳转路径
        SubscribeMessage subMassageObject = createSubMassageObject(req.getProjectId(), req.getPageUrl(), MassageTemplateConst.MASSAGE_TYPE_AUDIT, MassageTemplateConst.SUBSCRIBE_AUDIT_TEMPLATE_ID);
        subscribeMassageMapper.addSubscribeMassage(subMassageObject);
    }

    /**
     * 项目创建者审核投递人
     * @param req
     */
    @Transactional
    @Override
    public void JoinMassageSubscribe(JoinSubscribeMassageParam req) {
       //加入项目审核通过 消息查看url 项目id openid(接受消息放) 名字审核人名字
        SubscribeMessage subMassageObject = createSubMassageObject(req.getProjectId(), req.getPageUrl(), MassageTemplateConst.MASSAGE_TYPE_PROJECT_AUDIT, MassageTemplateConst.PROJECT_DELIVERY_TEMPLATE_ID);
        subscribeMassageMapper.addSubscribeMassage(subMassageObject);
    }

    //创建加入项目对象
    protected SubscribeMessage createSubMassageObject(Integer projectId,String url,Integer type,String templateId) {
        //获取当前用户openid
        Date date = new Date();
        Map<String, Object> userInfo = ThreadLocalUtil.get();
        String openid = (String) userInfo.get("openid");
        SubscribeMessage subscribeMessage = new SubscribeMessage();
        subscribeMessage.setOpenId(openid);
        subscribeMessage.setProjectId(projectId);
        subscribeMessage.setSubPathUrl(url);
        subscribeMessage.setSendMessage(false);
        subscribeMessage.setCreateTime(date);
        subscribeMessage.setTemplateId(templateId);
        subscribeMessage.setMessageType(type);
        return subscribeMessage;
    }
}
